/****************************************************
          File Name:uid-euid-suid.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Wed 03 Sep 2014 04:03:12 PM CST
*****************************************************/
#define _GNU_SOURCE
#include<errno.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
/*
 * real uid: 进程的执行者 uid
 * effective uid: 进程执行时对文件的访问权限
 * saved set uid: euid的拷贝,用于恢复原有的euid(仅在euid改变时保存)
 *
 * 都是相对进程而言
 *
 * 不包括 chown root filename; chmod u+s filename
 * 即 set user id bit
 * */
int main()
{
    uid_t uid, euid, suid;
    uid_t old_uid, old_euid;

    uid=getuid();   old_uid=uid;
    euid=geteuid(); old_euid=euid;
    printf("uid %i euid %i\n",uid,euid);

    setreuid(uid,euid);
    if(errno)
        perror("setreuid");
    errno=0;
    printf("uid %i euid %i\n",uid,euid);

    setuid(2000);
    if(errno)
        perror("setuid");
    printf("uid %i euid %i\n",uid,euid);
    getresuid(&uid,&euid,&suid);
    printf("real uid: %i, eff uid: %i, set uid: %i\n",uid,euid,suid);

    return 0;
}
